'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en' ], function($) {
  return {
    init : function(){
    
      this.create();
      this.changeStatus();
      this.branches();
      this.resetPassword();
      this.bindSite();
      this.bindRelation();
      this.bindChangeManagedSites();
    },
    
    create : function(){
      
      var that = this;
      
      $(document).on("shown.bs.modal" , "#create-normal-principal, #create-site-service", function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        })
        
      })
      
      $(document).on("shown.bs.modal" , "#create-coach", function(e){
    	
    	var $wechatValidate = {'wechat_number' : function(value) { return that.validateWechatNumber(value); }}
      	$.extend($.bt_validate.fn, $wechatValidate)
      	$.extend($.bt_validate.text, {'wechat_number' : '请输入正确的微信号格式!'})
      	
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            $(e.currentTarget).find("form").find(".salary-group").each(function(e){
              var $courseId = $(this).find('input[name="courseIds"]') .val().trim()
              var $coachSalary = $(this).find('input[name="salaries"]').val().trim()
              $(this).find('.col-xs-6').append('<input type="hidden" name="coachSalaries">');
              $(this).find('input[name="coachSalaries"]').val($courseId + "|" + $coachSalary);
            })
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        })
        
      })
      
      $(document).on("shown.bs.modal" , "#create-area-manager, #create-site-manager", function(e){
        $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        })
        
      });
      
      $(document).on("shown.bs.modal" , "#create-area-server", function(e){
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate();  //bind validate
          var $form = $(e.currentTarget).find("form")                          //bind shiftform
          
          $form.shiftform({
            beforeSubmit : function(el) {
              $form.trigger("validate.ajax.submit");
              return $.bt_validate.result[$.bt_validate.form_id];
            }
          })
          
        })
    },
    
    changeStatus : function(){
      $(document).on("shown.bs.modal" , "#change-status" , function(e){
        $(e.currentTarget).find("form").shiftform() 
      });
      
    },
    
    branches : function(){
      
      $(document).on("shown.bs.modal" , "branches" , function(e){
        
      var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        });
      
      });
      
    },
    
    resetPassword : function(){
      
      $(document).on("shown.bs.modal" , "#reset-password" , function(e){
        
        $(e.currentTarget).find("form").shiftform() 
      
      });
      
    },
    
    bindSite : function(){
        
        $(document).on("shown.bs.modal" , "#bind-site" , function(e){
          
          var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id];
              }
            });
          
        });
        
    },
    
    bindRelation : function(){
        $(document).on("shown.bs.modal" , "#bind-relation" , function(e){
            $(e.currentTarget).find("form").shiftform()
        });
    },
    
    validateWechatNumber: function(value) {
        var result = true;
        
        if(value != '') {
          var patrn = "/^[a-zA-Z]+[a-zA-Z\d_-]{5,19}$/";
          if(!/^[a-zA-Z]+[a-zA-Z\d_-]{5,19}$/.test(value)) {
            result = false;
          }
        }
          return result;
    },
    
    bindChangeManagedSites : function(){
        $(document).on("shown.bs.modal" , "#change-managed-sites" , function(e){
            $(e.currentTarget).find("form").shiftform()
        });
    }
  }
})

